package com.gmail.RichardNguyenCS;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class SetupGUI {
	
	private JFrame frameSetupWindow = new JFrame();
	
	//Setup Window Variables
	private JPanel setupPanel;
	JComboBox<String> cbBaudRate;
	JSpinner spnDataBits;
	JSpinner spnStopBits;
	JComboBox <String> cbParityOption = new JComboBox();
	JLabel lblBaudRate = new JLabel("Baud Rate");
	JLabel lblDataBits = new JLabel("Data Bits");
	JLabel lblStopBits = new JLabel("Stop Bits");
	JLabel lblParityOption = new JLabel("Parity Option");
	JComboBox comboBox = new JComboBox();
	private JComboBox cbComPort;
	JLabel lblComPort = new JLabel("COM Port");
	JButton btnDefaultSerial = new JButton("Default");
	JButton btnOkSerial = new JButton("OK");
	JButton btnRefreshComPort = new JButton("Refresh");
	JButton btnSaveSerial = new JButton("Save");
	private JButton btnLoadSerial;
		

	
	public void buildSetupWindow() {
		//http://stackoverflow.com/questions/15556893/how-to-open-a-new-jframe-window-when-jmenuitem-is-clicked
		frameSetupWindow = new JFrame();
		frameSetupWindow.setSize(400,500);
		frameSetupWindow.setLocationRelativeTo(null);
		frameSetupWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameSetupWindow.setVisible(true);
		
		//Setup the Panel
		setupPanel = new JPanel();
		frameSetupWindow.getContentPane().add(setupPanel, BorderLayout.CENTER);
		setupPanel.setLayout(null);
		
		

		cbBaudRate = new JComboBox<String>();
		cbBaudRate.setModel(new DefaultComboBoxModel<String>(new String[] {"1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200", "230400"}));
		cbBaudRate.setSelectedItem("9600");
		cbBaudRate.setBounds(60, 117, 143, 20);
		setupPanel.add(cbBaudRate);

				
		spnDataBits = new JSpinner();
		spnDataBits.setModel(new SpinnerNumberModel(new Byte((byte) 8), new Byte((byte) 5), new Byte((byte) 8), new Byte((byte) 1)));//value, min, max, step
		spnDataBits.setBounds(60, 173, 143, 20);
		setupPanel.add(spnDataBits);
		

		//COM Port Number
		cbComPort = new JComboBox();
		cbComPort.setBounds(60, 62, 143, 20);
		setupPanel.add(cbComPort);
		
		lblComPort = new JLabel("COM Port");
		lblComPort.setBounds(60, 38, 69, 14);
		setupPanel.add(lblComPort);

		//Refresh Button
		btnRefreshComPort = new JButton("Refresh");
		btnRefreshComPort.setBounds(247, 61, 89, 23);
		setupPanel.add(btnRefreshComPort);
		
		//Baud Rate
		lblBaudRate = new JLabel("Baud Rate");
		lblBaudRate.setBounds(60, 93, 69, 14);
		setupPanel.add(lblBaudRate);
		
		//Stop Bits
		spnStopBits = new JSpinner();
		spnStopBits.setModel(new SpinnerNumberModel(new Byte((byte) 1), new Byte((byte) 1), new Byte((byte) 2), new Byte((byte) 1)));//value, min, max, step
		spnStopBits.setBounds(60, 229, 143, 20);
		setupPanel.add(spnStopBits);
		
		lblStopBits = new JLabel("Stop Bits");
		lblStopBits.setBounds(60, 204, 69, 14);
		setupPanel.add(lblStopBits);
				
		//Parity Bits
		cbParityOption = new JComboBox<String>();
		cbParityOption.setModel(new DefaultComboBoxModel<String>(new String[] {"None", "Even", "Odd", "Mark", "Space"}));
		cbParityOption.setBounds(60, 283, 143, 20);
		setupPanel.add(cbParityOption);
		
		lblParityOption = new JLabel("Parity Option");
		lblParityOption.setBounds(60, 260, 87, 14);
		setupPanel.add(lblParityOption);

		//Data Bits
		lblDataBits = new JLabel("Data Bits");
		lblDataBits.setBounds(60, 148, 69, 14);
		setupPanel.add(lblDataBits);
		
		//Load Button
		btnLoadSerial = new JButton("Load");
		btnLoadSerial.setBounds(60, 327, 89, 23);
		setupPanel.add(btnLoadSerial);

		//Default button
		btnDefaultSerial = new JButton("Default");
		btnDefaultSerial.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearSetupWindow();
			}
		});
		btnDefaultSerial.setBounds(249, 327, 89, 23);
		setupPanel.add(btnDefaultSerial);
		
		//Save Button
		btnSaveSerial = new JButton("Save");
		btnSaveSerial.setBounds(60, 372, 89, 23);
		setupPanel.add(btnSaveSerial);
		
		//OK Button
		btnOkSerial = new JButton("OK");
		btnOkSerial.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getCommSettings();//debug
			}
		});
		btnOkSerial.setBounds(249, 372, 89, 23);
		setupPanel.add(btnOkSerial);
		

	
	}//buildSetupWindow()
	
	private void clearSetupWindow() {
		cbBaudRate.setSelectedItem("9600");
		cbParityOption.setSelectedItem("None");
		spnDataBits.setValue((byte) 8);
		spnStopBits.setValue((byte) 1);
	}//clearSetupWindow
	
	public void getCommSettings() {
		String comPortValue = (String) cbComPort.getSelectedItem();
		String baudRateValue = (String) cbBaudRate.getSelectedItem();
		String parityOptionValue = (String) cbParityOption.getSelectedItem();
		Byte dataBitsValue = (Byte) spnDataBits.getValue();
		Byte stopBitsValue = (Byte) spnStopBits.getValue();
		
		System.out.println(comPortValue);//debug
		System.out.println(baudRateValue);//debug
		System.out.println(parityOptionValue);//debug
		System.out.println(dataBitsValue);//debug
		System.out.println(stopBitsValue);//debug
		
		
		frameSetupWindow.dispose();//close the Jframe window
	}//getCommSettings()
}
